using System;
using System.Collections.Generic;
using System.Text;

namespace TaugeFramework.Classes
{
    class TaugeEncryption
    {
        public const string HEXCHAR = "0123456789ABCDEF";
        public const string CHARHEX_MIN = "ABCDEFGHIJKLMNO";
        public const string CHARHEX_EQUAL = "P";
        public const string CHARHEX_PLUS = "QRSTUVWXYZ12345";

        

        public static string hrEncrypt(string target, string key)
        {
            string s1;
            string s2;
            string result;

            s1 = target.Trim();
            s2 = key;
            if (target.Length < key.Length)
                s1 = target.PadRight(key.Length, ' ');
            else
                s2 = key.PadRight(target.Length, ' ');

            s1 = strToHex(s1);
            s2 = strToHex(s2);

            result = "";
            for (int i = s1.Length - 1; i >= 0; i--)
                result = result + hexSelisih(s1.Substring(i, 1), s2.Substring(i, 1));
            return result;

        }

        public static string hrDecrypt(string target, string key)
        {
            string s1;
            string s2;
            int nlen;
            string result;

            s1 = target;
            s2 = key;
            nlen = target.Length / 2;
            if (key.Length < nlen)
                s2 = key.PadRight(nlen, ' ');
            else
                s1 = target.PadLeft(key.Length * 2, 'P');

            s1 = reverseString(s1);
            s2 = strToHex(s2);

            result = "";
            for (int i = 0; i < s1.Length; i++)
                result = result + hexTambah(s1.Substring(i, 1), s2.Substring(i, 1));

            return hexToStr(result).Trim();
        }

        public static string hexToStr(string s)
        {
            string result = "";
            for (int i = 0; i < s.Length; i += 2)
                result = result + Convert.ToChar(hexToInt(s.Substring(i, 2)));
            return result;
        }

        public static string strToHex(string s)
        {
            string result = "";
            for (int i = 0; i < s.Length; i++)
                result = result + intToHex(Convert.ToInt32(Convert.ToChar(s.Substring(i, 1))));
            return result;
        }

        public static string intToHex(int n)
        {
            int n1;
            int n2;
            n1 = n / 16;
            n2 = n % 16;
            return HEXCHAR.Substring(n1, 1) + HEXCHAR.Substring(n2, 1);
        }

        public static int hexToInt(string s)
        {
            return HEXCHAR.IndexOf(s.Substring(0, 1)) * 16 + HEXCHAR.IndexOf(s.Substring(1, 1));
        }

        public static string hexTambah(string c1, string c2)
        {
            int n1;
            int n2;

            if (c1 == "P")
                return c2;
            else
            {
                n1 = CHARHEX_MIN.IndexOf(c1) + 1;
                if (n1 > 0)
                    n1 *= -1;
                else
                    n1 = CHARHEX_PLUS.IndexOf(c1) + 1;
            }

            n2 = HEXCHAR.IndexOf(c2);

            if (n1 + n2 > 15)
                return "";
            else
                return HEXCHAR.Substring(n1 + n2, 1);
        }

        public static string hexSelisih(string c1, string c2)
        {
            int n1;
            int n2;
            int n;

            n1 = HEXCHAR.IndexOf(c1);
            n2 = HEXCHAR.IndexOf(c2);

            n = n1 - n2;
            if (n < 0)
                return CHARHEX_MIN.Substring((n * -1) - 1, 1);
            else if (n == 0)
                return CHARHEX_EQUAL;
            else
                return CHARHEX_PLUS.Substring(n - 1, 1);


        }

        public static string reverseString(string s)
        {
            string result = "";
            for (int i = s.Length - 1; i >= 0; i--)
                result = result + s.Substring(i, 1);
            return result;
        }


        public static int generateCheckSum(params string[] astr)
        {
            int nresult = 0;
            string stmp;

            foreach (string str in astr)
            {
                for (int i = 0; i < str.Length; i++)
                {
                    stmp = Convert.ToInt32(Convert.ToChar(str.Substring(i, 1).ToUpper())).ToString();
                    for (int j = 0; j < stmp.Length; j++)
                        nresult = nresult + Convert.ToInt32(stmp.Substring(j, 1));
                }
            }
            return nresult;

        }


    }
}
